package gminet.infra.toolkit;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

/**
 * @author: Gabriel Flores Mendes
 */
public final class NumberToolkit {

    private static final Locale LOCALE = new Locale("pt", "br");

    private static NumberFormat[] numberFormat = new NumberFormat[6];

    /**
     * ConversorToolkit constructor comment.
     */
    private NumberToolkit() {
        super();
    }

    public static int getIntFromString(String strValue) {

        return getIntFromString(strValue, 0);
    }

    public static int getIntFromString(String strValue, int defaultValue) {

        int intValue;
        try {
            intValue = Integer.parseInt(strValue);
        } catch (Exception e) {
            intValue = defaultValue;
        }
        return intValue;
    }

    public static double getDoubleFromString(String value) throws ParseException {
        try {
            // coloca String no formato americano
            String auxValue = value;
            for (int i = auxValue.indexOf('.'); i != -1; i = auxValue.indexOf('.')) {
                auxValue = auxValue.substring(0, i) + auxValue.substring(i + 1);
            }
            return Double.valueOf(auxValue.replace(',', '.')).doubleValue();
        } catch (NumberFormatException e) {
            throw new RuntimeException("Invalid double value (" + value + ")");
        }
    }

    public static float getFloatFromString(String value) throws ParseException {
        try {
            // coloca String no formato americano
            String auxValue = value;
            for (int i = auxValue.indexOf('.'); i != -1; i = auxValue.indexOf('.')) {
                auxValue = auxValue.substring(0, i) + auxValue.substring(i + 1);
            }
            return Float.valueOf(auxValue.replace(',', '.')).floatValue();
        } catch (NumberFormatException e) {
            throw new RuntimeException("Invalid double value (" + value + ")");
        }
    }

    public static String getStringFromDouble(double value) {

        return getStringFromDouble(value, 2);
    }

    public static String getStringFromDouble(double value, int digits) {

        return getNumberFormat(digits).format(value);
    }

    public static String getStringFromInt(int value, int length) {

        String strValue = String.valueOf(value);
        while (strValue.length() < length) {
            strValue = "0" + strValue;
        }
        return strValue;
    }

    private static NumberFormat getNumberFormat(int digits) {

        if (digits > 5) {
            throw new RuntimeException("Invalid digits value. Max digits value is 5.");
        }
        if (numberFormat[digits] == null) {
            NumberFormat nf = NumberFormat.getInstance(LOCALE);
            nf.setMaximumFractionDigits(digits);
            nf.setMinimumFractionDigits(digits);
            numberFormat[digits] = nf;
        }
        return numberFormat[digits];
    }

}
